# -*- coding: utf-8 -*-
import os
import types
import json
import pprint
from datetime import datetime
import uuid
import unicodedata


def dump(value):
    '''格式化打印对象'''
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(value)

def json_dump(value):
    '''json格式打印对象'''
    json_str = json.dumps(value, indent=4)
    print(json_str)

def print_now(prefix=''):
    '''
    打印当前时间(%Y-%m-%d %H:%M:%S.%f)
    返回当前时间
    '''
    now = datetime.now()
    now_str = now.strftime('%Y-%m-%d %H:%M:%S.%f')
    print('{0}{1}'.format(prefix, now_str))
    return now

def unicode_len(value):
    '''获取unicode字符串输出占用长度'''
    length = 0
    for c in value:
        udn = unicodedata.name(c)
        if udn.startswith('CJK'):
            length += 2
        else:
            length += 1
    return length


def center_print(value, length=80, fill=' '):
    ul = unicode_len(value)
    if ul < length:
        diff = length - ul
        prefix = diff // 2
        new_value = value.ljust(prefix + ul, fill).rjust(length, fill)
        print(new_value)
    else:
        print(value)

def sibling_path(reference, file_name):
    '''
    根据文件名和参考文件路径，得到与参考文件同级的路径
    '''
    abs_path = os.path.abspath(reference)
    dir_path = os.path.dirname(abs_path)
    return os.path.join(dir_path, file_name)

def random_uuid():
    '''获取随机uuid'''
    ts = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    return uuid.uuid5(uuid.NAMESPACE_DNS, ts).hex

def safe_div(dividend, divisor):
    '''安全除法，防止被0除'''
    return dividend / divisor if divisor != 0 else 0